﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using memoVM;
using Microsoft.Win32;
using System.IO;

namespace memoUI
{
    /// <summary>
    /// Interaction logic for SubTopicEditorWindow.xaml
    /// </summary>
    public partial class SubTopicEditorWindow : Window
    {        
        public SubTopicEditorWindow(bool isNew)
        {
            IsNew = isNew;

            InitializeComponent();

            _ViewModel = ViewModel.Instance;

            Init();
        }

        private void SubtopicImage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "Image files (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == true)
            {
                IconFilePath = openFileDialog1.SafeFileName;
                SubtopicImage.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(ImagePath + IconFilePath);
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if ((NameTextBox.Text != string.Empty) || (!string.IsNullOrWhiteSpace(NameTextBox.Text)))
            {
                if (IsNew == true)
                {
                    _ViewModel.SaveSubtopic(NameTextBox.Text, DescriptionTextBox.Text, IconFilePath);
                }
                else
                {
                    _ViewModel.EditSubtopic(NameTextBox.Text, DescriptionTextBox.Text, IconFilePath);
                }
                
                this.Close();
            }
            else
            {
                MessageBox.Show("SubtopicName (╯°□°）╯︵ ┻━┻");
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public void Init()
        {
            if (IsNew == true)
            {
                NameTextBox.Text = "";
                DescriptionTextBox.Text = "";
                IconFilePath = "DEFAULT.png";
            }
            else
            {
                NameTextBox.Text = _ViewModel.GetSelectedSubtopic().Title;
                DescriptionTextBox.Text = _ViewModel.GetSelectedSubtopic().ShortDescription;
                IconFilePath = _ViewModel.GetSelectedSubtopic().Icon;
            }
            
            SubtopicImage.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(ImagePath + IconFilePath);
        }

        private ViewModel _ViewModel;
        private bool IsNew;
        private string IconFilePath;        
        private string ImagePath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "/Images/";
    }
}
